/*
 * @Date: 2023-03-17 15:41:09
 * @LastEditors: Chuying
 * @LastEditTime: 2023-03-28 15:16:13
 * @Description:
 */
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  HttpCode,
  UseGuards,
  Req,
  UsePipes,
} from '@nestjs/common';
import { Request } from 'express';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { NoAuth } from 'src/common/decorator/no-auth.decorator';
import { Role } from 'src/common/decorator/role/role.decorator';
import { JwtAuthGuard } from 'src/modules/account/auth/guards/jwt-auth.guard';
import * as Joi from '@hapi/joi';
import { UserInfoPipe } from './pipe/userInfo.pipe';
import { CommonService } from 'src/modules/common/common.service';
import {
  ApiBearerAuth,
  ApiCreatedResponse,
  ApiOperation,
  ApiTags,
} from '@nestjs/swagger';
import { UserInfo } from './entities/user_info.entity';
import { ApiPaginatedResponse } from 'src/decorator/api-paginated-response.decorator';
import { FollowDto } from './dto/follow-dto';

const infoSchema = Joi.object().keys({
  real_name: Joi.string().required(),
  gender: Joi.string().required(),
  phone: Joi.string().required(),
  avatar: Joi.string().required(),
  vip: Joi.number().integer().min(0).max(4),
});
// @ApiBearerAuth()
@ApiTags('用户管理')
@Controller('user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly commonService: CommonService,
  ) {}

  @ApiOperation({
    description: '新用户注册',
    summary: '用户注册',
  })
  @NoAuth()
  @HttpCode(200)
  @Post('create')
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  @ApiOperation({
    description: '获取用户列表',
    summary: '获取用户列表',
    deprecated: true,
  })
  @Get('list')
  @Role('admin')
  // @UseGuards(JwtAuthGuard)
  findAll() {
    return this.userService.findAll();
  }

  //   @ApiOperation({
  //     description:'获取用户详细信息',
  //     summary:'获取用户详细信息'
  //   })
  //   @Get('query/:id')
  //   findOne(@Query() params, @Param('id') id: string) {
  //     console.log(params);
  //     return this.userService.findOne(+id);
  //   }

  @ApiOperation({
    description: '更改账号密码',
    summary: '更改账号密码',
  })
  @Patch('update/:id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @ApiOperation({
    description: '删除用户',
    summary: '删除用户',
  })
  @Delete('delete/:id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }

  @ApiOperation({
    description: '更改用户信息',
    summary: '更改用户信息',
  })
  @ApiCreatedResponse({
    type: [UserInfo],
    description: '用户信息',
  })
  //   @ApiPaginatedResponse(UserInfo)
  @Post('change/info')
  @UsePipes(new UserInfoPipe(infoSchema))
  changeInfo(@Body() info, @Req() req: Request) {
    // console.log(info, '---------');
    // console.log(req.user, '-----');
    return this.userService.changeInfo(info, req.user);
  }

  @Post('follow/update')
  follow(@Body() info: FollowDto) {
    return this.userService.follow(info, 'update');
  }

  @Patch('follow/cancel')
  cancelfollow(@Body() info: FollowDto) {
    return this.userService.follow(info, 'cancel');
  }
}
